package com.hudilao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hudilao.entity.DiningTable;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface DiningTableMapper extends BaseMapper<DiningTable> {
    /**
     * 通过桌号查询餐桌状态
     * @param tableNumber 餐桌号
     * @return 餐桌状态status
     */
    @Select("select status from dining_table where table_number = #{tableNumber}")
    Byte getStatusByTableNumber( int tableNumber);

    /**
     * 更新餐桌状态
     * @param tableNumber 餐桌号
     */
    @Select("update dining_table set status=0 where table_number = #{tableNumber}")
    void updateStatusByTableNumber(int tableNumber);

    /**
     * 根据顾客id查找其当前在使用的桌号
     * @param customerId  顾客id
     * @return 桌号
     */
//    @Select("select table_number from dining_table where customer_id=#{customerId}")
    @Select("SELECT dt.table_number " +
            "FROM dining_table dt " +
            "LEFT JOIN customer c ON dt.customer_id = c.id " +
            "WHERE dt.customer_id = #{customerId}")
    int getTableNumberByCustomerID(@Param("customerId") long customerId);

    /**
     * 根据顾客id获取其使用的餐桌
     * @param customerId 顾客id
     * @return 餐桌
     */
    @Select("SELECT * " +
            "FROM dining_table dt " +
            "JOIN customer c ON dt.customer_id = c.id " +
            "WHERE dt.customer_id = #{customerId}")
    DiningTable getTableByCustomerId(long customerId);

    /**
     * 根据桌号绑定当前正在使用的顾客
     * @param tableNumber  桌号
     * @param customerId  顾客id
     */
    @Update("UPDATE dining_table dt " +
            "LEFT JOIN customer c ON dt.customer_id = c.id " +
            "SET dt.customer_id = #{customerId} " +
            "WHERE dt.table_number = #{tableNumber}")
    void updateCustomerIdByTableNumber(@Param("tableNumber") int tableNumber, @Param("customerId") long customerId);

    @Update("UPDATE dining_table dt " +
            "LEFT JOIN customer c ON dt.customer_id = c.id " +
            "SET dt.customer_id = null,dt.status = 1 " +
            "WHERE dt.customer_id = #{customerId}")
    void updateStatusAfterPayment(long customerId);
}
